<?php

// We'll store final bread crumbs into an array

$breadCrumbs = array();


// Add the home page to the bread crumb

$breadCrumbs[] = array
(
	'title' => str(pref('[Loop]/views/pathToPageHome.html')),
	'url' => app()->getPathHelper()->convertPathToUrl(pref('[Loop]/views/pathToPageHome.html'))
);


// Is it the home page? If not we must compute the view name

if (view()->getPath() != pref('[Loop]/views/pathToPageHome.html'))
{
	// Explode the view name into fragments
	
	$nameFragments = explode('.', view()->getName());
	$nbNameFragments = count($nameFragments);
	
	// Parse each name fragment
	
	$name = null;
	
	for ($i = 0; $i < $nbNameFragments; $i++)
	{
		// Get the name fragment
		
		$nameFragment = $nameFragments[$i];
		
		
		// Prepare the path and URL
		
		$name .= $nameFragment . '.';
		
		$path = '/Html/Page.' . $name . 'html';
		$url = app()->getPathHelper()->convertPathToUrl($path, null, false);
	

		// If it's the last fragment, add the GET arguments to the URL
		
		$j = strpos(v('SERVER::REQUEST_URI'), '?');
		
		if ($j > 0)
		{
			$url .= substr(v('SERVER::REQUEST_URI'), $j);
		}


		// Add the URL and translation at an odd index
		
		$breadCrumbs[($i * 2) + 1] = array
		(
			'title' => str($path),
			'url' => $url
		);
	}
}

// Let's inject view objects if there are any

$objects = array_reverse(objects());
$i = 0;

foreach ($objects as $object)
{
	// Add the breadcrum at an even index
	
	$breadCrumbs[($i * 2) + 2] = array
	(
		'title' => $object->getDefaultName(),
		'url' => $object->getUrlOfPage(ACTION_VIEW)
	);
	
	$i++;
}

// Sort the array so that name fragments and view objects are in the right order

ksort($breadCrumbs);


// Remove duplicates

$lastUrl = null;

foreach ($breadCrumbs as $i => $breadCrumb)
{
	if ($breadCrumb['url'] == $lastUrl)
	{
		unset($breadCrumbs[$i]);
	}
	
	$lastUrl = $breadCrumb['url'];
}


// Prepare variables for the view

v('breadCrumbs', $breadCrumbs);

?>
